package com.tai.cobrapp.activity;

import com.tai.cobrapp.R;
import com.tai.cobrapp.adapter.DetailSurveyListAdapter;
import com.tai.cobrapp.backend.Backend;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class DetailSurveyActivity extends ListActivity{

	DetailSurveyListAdapter adapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_surveys);
		
		if( this.getIntent().getExtras() != null ){
			Integer idRelev = (Integer)this.getIntent().getExtras().get("idRelevamiento");
			//Toast.makeText(this, "User id " + Usuario.getUserInstance().getId() , Toast.LENGTH_SHORT).show();

		adapter = new DetailSurveyListAdapter(this, Backend.getInstance().getDetailedSurvey(idRelev, this));
		this.getListView().setAdapter(adapter);
		
		}
		this.getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// aca obtener el id del elemento y luego ir a editarlo por ejemplo, o mostrar detalles
				// pasandole el ID como extras a otra actividad
				//Log.d("Click", ((DetalleRelevamiento)adapter.getItem(position)).getDispositivo().getName().toString());
				
				//Intent i = new Intent(MainActivity.this, DetailActivity.class);
				// Le paso el objeto completo DEL TIPO SERIALIZABLE
				//i.putExtra("dispositivo", ((DetalleRelevamiento)adapter.getItem(position)));
			}
		
		});
	}
	@Override
	public void onBackPressed() {
		Intent i = new Intent(DetailSurveyActivity.this, ListSurveysActivity.class);
		startActivity(i);
		super.onBackPressed();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	@Override
	protected void onStop() {
		finish();
		super.onStop();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.main_menu:
			Intent i = new Intent(this, OptionsMenuActivity.class);
			startActivity(i);
			break;
		case R.id.log_out:
			Intent i2 = new Intent(this, LoginActivity.class);
			startActivity(i2);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
